<template>
  <div>
    <h3>医疗服务管理</h3>

    <!-- 查询功能 -->
    <el-input v-model="searchQuery" placeholder="请输入服务名称进行查询" style="width: 300px; margin-bottom: 20px;" clearable></el-input>
    <el-button type="primary" @click="fetchMedicalServices">查询</el-button>

    <!-- 添加医疗服务按钮 -->
    <el-button type="primary" @click="openDialog(null)">添加医疗服务</el-button>

    <!-- 医疗服务列表 -->
    <el-table :data="filteredMedicalServices" style="width: 100%">
      <el-table-column prop="medicalserviceid" label="医疗服务ID" width="180"></el-table-column>
      <el-table-column prop="treatment" label="服务名称" width="180"></el-table-column>
      <el-table-column prop="totalcost" label="费用" width="150"></el-table-column>
      <el-table-column prop="servicedate" label="服务日期" width="180"></el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="openDialog(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteMedicalService(scope.row.medicalserviceid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑医疗服务对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible">
      <el-form :model="medicalServiceForm" label-width="120px">
        <el-form-item label="医疗服务ID">
          <el-input v-model="medicalServiceForm.medicalserviceid" :disabled="isEditing"></el-input>
        </el-form-item>
        <el-form-item label="服务名称">
          <el-input v-model="medicalServiceForm.treatment"></el-input>
        </el-form-item>
        <el-form-item label="费用">
          <el-input v-model="medicalServiceForm.totalcost"></el-input>
        </el-form-item>
        <el-form-item label="服务日期">
          <el-date-picker v-model="medicalServiceForm.servicedate" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveMedicalService">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      searchQuery: '', // 搜索关键词
      medicalServiceList: [],
      medicalServiceForm: {
        medicalserviceid: '',
        treatment: '',
        totalcost: '',
        servicedate: ''
      },
      dialogVisible: false,
      dialogTitle: '',
      isEditing: false
    };
  },
  computed: {
    filteredMedicalServices() {
      return this.medicalServiceList.filter(service =>
          service.treatment.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  },
  mounted() {
    this.fetchMedicalServices()
  },
  methods: {
    // 获取医疗服务列表
    fetchMedicalServices() {
      axios.get('http://localhost:8081/medicalService/all')
          .then(response => {
            this.medicalServiceList = response.data
          })
          .catch(error => {
            console.error("Error fetching insured persons:", error);
          });
    },
    // 打开添加/编辑对话框
    openDialog(row) {
      if (row) {
        this.isEditing = true;
        this.dialogTitle = '编辑医保审批';
        this.medicalServiceForm = {...row};
      } else {
        this.isEditing = false;
        this.dialogTitle = '添加医保审批';
        this.resetForm();
      }
      this.dialogVisible = true;
    },
    // 重置表单
    resetForm() {
      this.medicalServiceForm = {
        medicalserviceid: '',
        treatment: '',
        totalcost: '',
        servicedate: ''
      };
    },
    // 保存医疗服务
    saveMedicalService() {
      if (this.isEditing) {
        axios.put(`http://localhost:8081/medicalService/update`, this.medicalServiceForm)
            .then(() => {
              this.fetchMedicalServices();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error updating insured person:", error);
            });
      } else {
        axios.post('http://localhost:8081/medicalService/add', this.medicalServiceForm)
            .then(() => {
              this.fetchMedicalServices();
              this.dialogVisible = false;
            })
            .catch(error => {
              console.error("Error adding insured person:", error);
            });
      }
    },
    // 删除医疗服务
    deleteMedicalService(medicalserviceid) {
      axios.delete(`http://localhost:8081/medicalService/delete/${medicalserviceid}`)
          .then(() => {
            this.fetchMedicalServices();
          })
          .catch(error => {
            console.error(error);
          });
    }
  }
};
</script>

<style scoped>
.el-button {
  margin-bottom: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
